<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增或编辑角色</title>
    <link th:replace="template/sys_header :: header"/>
</head>
<body>
<form lay-filter="form1" class="layui-form margintop20" action="">
    <input type="hidden" id="roleId" name="roleId" value=""/>
    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>角色名称
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="roleName" name="roleName" lay-verify="required" autocomplete="" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux" style="margin-left: 5px;">
            <span class="x-red">*</span>请输入角色名称
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            角色编号
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="roleCode" name="roleCode"  autocomplete="" class="layui-input">
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            排序号
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="orderNum" name="orderNum" lay-verify="number" autocomplete="" class="layui-input">
        </div>
    </div>

    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            备注
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="remark" name="remark"  autocomplete="" class="layui-input">
        </div>
    </div>

    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">

        </div>
        <div class="layui-col-sm5" style="text-align: center">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save"><i class="iconfont">&#xe747;</i>
                &nbsp;保存&nbsp;
            </button>
            &nbsp;&nbsp;
            <button class="layui-btn layui-btn-danger" onclick="closeWindow()"><i class="iconfont">&#xe747;</i>
                &nbsp;关闭&nbsp;
            </button>
        </div>
    </div>
</form>
<script type="text/javascript" th:replace="template/sys_header :: footer"></script>
<script type="text/javascript" th:inline="javascript">
    var $ = layui.jquery, form = layui.form;
    var role = [[${role}]];//角色信息
    var isEdit = false;//默认不是编辑模式
    console.log(role);
    if(role && role.roleId){
        isEdit = true;//编辑
        form.val('form1', {
            "roleId" : role.roleId,
            "roleName": role.roleName,
            "roleCode": role.roleCode,
            "remark": role.remark,
            "orderNum": role.orderNum
        });
    }

    $(function () {
        form.on('submit(save)', function (data) {
            console.log(data.field);
            //发异步，数据提交
            if(isEdit){
                edit(data.field);
            }else{
                add(data.field);
            }
            return false;
        });
    })

    //关闭当前frame
    function closeWindow() {
        xadmin.close();
    }

    function add(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/role/add",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        var orgId = res.resultObj;
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist();
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }

    function edit(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "sys/role/edit",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist();
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }
</script>
</body>
</html>